var funArray = [{
    name: '对字符串进行MD5加密',
    key: "md5('string')",
    test: "md5('string')",
    code: `
const crypto = require("crypto");
const md5 = function (str) { 
    var md5 = crypto.createHash("md5");
    var md5Sum = md5.update(str);
    var result = md5Sum.digest("hex");
    return result;
}
    `,
}, {
    name: '生成GUID(随机生成)',
    key: "Guid()",
    test: "Guid()",
    code: `
var Guid = function () {
    var guid = '';
    for (var i = 1; i <= 32; i++) {
        var n = Math.floor(Math.random() * 16.0).toString(16);
        guid += n;
        if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
            guid += '-';
    }
    return guid;
};
    `,
}, {
    name: '生成UUID(随机生成)',
    key: 'UUID()',
    test: 'UUID()',
    code: `
// 这里可以写注释。。。
const uuid = require('uuid');
const UUID = function () { 
    return uuid.v1();
};
    `,
}, {
    name: '读取全局缓存',
    key: "getCache('key')",
    test: "getCache('key')",
    code: `
var getCache = function (key) { 
    if (window.hasOwnProperty('__GlobalCache__') && window.__GlobalCache__.hasOwnProperty(key) ) {
        return  window.__GlobalCache__[key];
    } else {
        return null;
    }
};
    `,
}, {
    name: '写入全局缓存',
    key: "setCache('key','value')",
    test: "setCache('key','value')",
    code: `
var setCache = function (key,value) { 
    if (!window.__GlobalCache__){
        window.__GlobalCache__ = {};
    };
    window.__GlobalCache__[key] = value;
    store.dispatch('cache/setFromCode',{
        key : key,
        value : value
    });
};
    `,
}];

const state = {
    funArray: [],
};
const mutations = {};
const getters = {};
const actions = {
    getAllCodeString(context) {
        var _string = ';';
        for (let i = 0; i < context.state.funArray.length; i++) {
            const element = context.state.funArray[i];
            _string += element.code + ';'
        }
        return _string;
    },
    getRenderData(context) {
        // action=> 获取全部Key值
        window.action.fun.getAll().then((res) => {
            if (res) {
                context.state.funArray = res;
            };
        });
    },
    creatFunction(context, param) {
        window.action.fun.creat({
            key: param.key,
            name: param.name,
            test: param.test,
            code: param.code,
        }).then((res) => {
            if (res) {
                param.callback();
                context.dispatch('getRenderData');
            };
        });
    },
    updateFunction(context, param) {
        window.action.fun.update({
            id: param.id,
            key: param.key,
            name: param.name,
            test: param.test,
            code: param.code,
        }).then((res) => {
            if (res) {
                param.callback();
                context.dispatch('getRenderData');
            };
        });
    },
    deleteFunction(context, id) {
        window.action.fun.delete(id).then((res) => {
            if (res) {
                context.dispatch('getRenderData');
            };
        });
    }
};

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}